package com.station.Activitys.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.station.Activitys.R;
import com.station.Activitys.activity.base.BaseActivity;
import com.station.Activitys.adapter.ImageDisplayMagAdapter;
import com.station.Activitys.bean.ImageJson;
import com.station.Activitys.bean.MyTest;
import com.station.Activitys.http.manager.WorkManager;
import com.station.Activitys.utils.CacheManger;
import com.station.Activitys.utils.Constant;
import com.station.Activitys.utils.ToastUtils;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;

/**
 * 项目名称：基站管理平台 手机app
 * 类描述：我的可验收信息 详情页面
 * 创建人：张飞祥
 * 创建时间：2018/10/17 14:15
 * 修改人：张飞祥
 * 修改时间：2018/10/17 14:15
 * 修改备注：
 */
public class MyTestMsgDetailsActivity extends BaseActivity {

    //TODO 标题
    //返回按钮
    @Bind(R.id.iv_return) ImageView ivReturn;
    //标题
    @Bind(R.id.tv_title) TextView tvTitle;

    //TODO 基站 基础信息
    //基站名称
    @Bind(R.id.tv_name) TextView tvName;
    //基站状态
    @Bind(R.id.tv_state) TextView tvState;
    //自动获取的经纬度
    @Bind(R.id.tv_gps) TextView tvGps;
    //手动输入的经纬度
    @Bind(R.id.tv_my_gps) TextView tvMyGps;
    //信号强度
    @Bind(R.id.tv_signal) TextView tvSignal;
    //物理站址名称
    @Bind(R.id.tv_physical_name) TextView tvPhysicalName;
    //物理站址地址
    @Bind(R.id.tv_physical_address) TextView tvPhysicalAddress;
    //省份
    @Bind(R.id.tv_province) TextView tvProvince;
    //城市
    @Bind(R.id.tv_city) TextView tvCity;
    //网格
    @Bind(R.id.tv_grid) TextView tvGrid;
    //站型
    @Bind(R.id.tv_station_model) TextView tvStationModel;
    //厂家
    @Bind(R.id.tv_vender) TextView tvVender;
    //区域类型
    @Bind(R.id.tv_area_type) TextView tvAreaType;
    //主场景类型
    @Bind(R.id.tv_main_scene_type) TextView tvMainSceneType;
    //优先级
    @Bind(R.id.tv_priority) TextView tvPriority;
    //工程期属
    @Bind(R.id.tv_project_num) TextView tvProjectNum;

    //TODO 图片
    //反填图片列表
    @Bind(R.id.rv_http_img_list) RecyclerView rvHttpImgList;

    /**
     * 变量
     */
    //初始化网络请求
    public WorkManager mApiManager;
    //工单 实体类
    private MyTest mMyTest = null;

    //图片反填数据适配器
    private ImageDisplayMagAdapter mHttpAdapter = null;

    @Override
    protected void initUI() {
        setContentView(R.layout.act_my_test_msg_details);
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getWorkApiManager(this, this.getTaskId());
    }

    @Override
    protected void initData() {

        //获取从上个页面 传递过来的 实体类
        Bundle bundle = this.getIntent().getExtras();
        //获取 实体类
        mMyTest = (MyTest) bundle.getSerializable(Constant.INTENT_MY_TEST_BEAN);
        //获取当前用户
        mUser = CacheManger.getUser(mACache);
        //解决 滑动冲突
        rvHttpImgList.setNestedScrollingEnabled(false);

        //设置标题
        tvTitle.setText("验收详情");
        //显示返回按钮
        ivReturn.setVisibility(View.VISIBLE);

        //TODO 填充基础数据
        setBasicsData();
    }

    /**
     * 填充基础数据
     */
    private void setBasicsData() {
        /**
         * 填充 数据
         */
        if (mMyTest != null) {
            //TODO 开始网络请求获取图片 数据
            initShowTimeDialog("正在加载数据，请稍后....", this);
            mApiManager.workMyTestMsgImages(mMyTest.getId());

            //基站名称
            tvName.setText(mMyTest.getNameOfOpenStationAddr());
            //基站状态
            tvState.setText(mMyTest.getState());
            //物理站址名称
            tvPhysicalName.setText(mMyTest.getPhysicalStationName());
            //物理站址地址
            tvPhysicalAddress.setText(mMyTest.getAddressOfPhysicalStation());
            //自动获取的经纬度
            tvGps.setText("经度："+mMyTest.getAutoGpsX()+"     纬度："+mMyTest.getAutoGpsY());
            //手动输入的经纬度
            tvMyGps.setText("经度："+mMyTest.getManualInputGpsX()+"     纬度："+mMyTest.getManualInputGpsY());
            //信号强度
            tvSignal.setText(mMyTest.getSignalIntensity()+" dbm");
            //省份
            tvProvince.setText(mMyTest.getProvince());
            //本地网
            tvCity.setText(mMyTest.getLocalNetworkAddress());
            //网格
            tvGrid.setText(mMyTest.getGrid());
            //站型
            tvStationModel.setText(mMyTest.getStationModel());
            //厂家
            tvVender.setText(mMyTest.getVender());
            //区域类型
            tvAreaType.setText(mMyTest.getAreaType());
            //主场景类型
            tvMainSceneType.setText(mMyTest.getMainSceneType());
            //工程期属
            tvProjectNum.setText(mMyTest.getNumOfProject());
            //优先级
            tvPriority.setText(mMyTest.getPriority());
        }
    }

    /**
     * 获取 我的验收信息图片列表 报错
     */
    @Subscriber(tag = Constant.BUS_WORK_MY_TEST_MSG_IMAGES_ERROR, mode = ThreadMode.MAIN)
    public void initHttpError(String httpError) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //销毁弹窗
                mTimeDialog.dismiss();
                //弹出提示
                ToastUtils.showShort(MyTestMsgDetailsActivity.this, Constant.NETWORK_ERROR_MSG);
            }
        }, 1000);
    }

    /**
     * 获取 我的验收信息图片列表
     */
    @Subscriber(tag = Constant.BUS_WORK_MY_TEST_MSG_IMAGES, mode = ThreadMode.MAIN)
    public void initHttpData(final List<ImageJson> list) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (list != null) {
                    //销毁弹窗
                    mTimeDialog.dismiss();
                    //TODO 图片反填列表
                    mHttpAdapter = new ImageDisplayMagAdapter(MyTestMsgDetailsActivity.this, list, false);
                    //添加 适配器 样式
                    rvHttpImgList.setLayoutManager(new LinearLayoutManager(MyTestMsgDetailsActivity.this, LinearLayoutManager.VERTICAL, false));
                    //添加 适配器
                    rvHttpImgList.setAdapter(mHttpAdapter);
                }
            }
        }, 1000);
    }
}
